<template>
    
    <view>
        <view class="datetime-picker">
            <text>选择日期：</text>
            <up-datetime-picker hasInput v-show="show"
             style="text-align: center;" :show="show"
              format="YYYY-MM" placeholder="请选择日期"
                mode="year-month" v-model="date_time" @confirm="changeDate"></up-datetime-picker>
        </view>
        <view class="table">
            <view class="th">
                <view class="th-item">
                    <text>薪资项</text>
                </view>
                <view class="th-item">
                    
                </view>
                <view class="th-item">
                    <text>金额</text>
                </view>
            </view>
            <view class="tbody" >
                <view class="tbody-item" v-for="(item, index) in tableData" :key="item.id">
                    <view class="item">{{ item.salary_name }}</view>
                    <view class="item"></view>
                    <view class="item">{{ item.nums }}</view>
                </view>
            </view>
        </view>
    </view>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { getPayList} from '@/api/table';
//控制日历的展示
const show = ref<boolean>(false);
//表格数据
const tableData = ref<any>([])
//选择日期
const changeDate=(timestamp:any)=>{
      // 将时间戳转换为 Date 对象
      const date = new Date(timestamp.value);
      // 格式化日期为 YYYY-MM
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
       getList(`${year}-${month}`)
    }
//获取当前日期
function getCurrentYearMonth() {
  const now = new Date();
  const year = now.getFullYear(); // 获取当前年份
  const month = String(now.getMonth() + 1).padStart(2, '0'); // 获取当前月份，padStart确保两位数格式
  return `${year}-${month}`;
}

//当前日期
const date_time = ref<any>(Date.now())
onMounted(()=>{
    getList(getCurrentYearMonth())
})
const getList=(date:string)=>{
    getPayList(date).then((res:any)=>{
            tableData.value = res.data.data;
            console.log(tableData.value)
    })
}
</script>
<style lang="scss" scoped>
    .table {
        width: 700rpx;
        margin-top: 32rpx;
        .th {
            display: grid;
            grid-template-columns: 2fr 1fr 2fr;
            height: 80rpx;
            border-top: 1px solid rgb(211, 211, 211);
            border-bottom: 1px solid rgb(211, 211, 211);
            .th-item {
                background-color: rgb(245, 246, 248);
                text-align: center;
                line-height: 80rpx;
                font-size: 32rpx;
               
                border-left: none; // 去掉左边框;
            }

            .th-item:first-child {
                border-left: 1px solid rgb(211, 211, 211); // 保留左边框;
            }
        }

        .tbody {
            transform: translateY(-1px);
            max-height: 70vh; 
            overflow: auto;
            width: 700rpx;
            .tbody-item:first-child {
                border-top: 1px solid rgb(211, 211, 211); 
            }
            .tbody-item:nth-child(2n){
                background-color: rgb(250, 250, 250);
            }
            .tbody-item {
                display: grid;
                grid-template-columns: 2fr 1fr 2fr;    
                background-color: #fff;
                border-bottom: 1px solid rgb(211, 211, 211);
                .item{
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    padding: 16rpx 8rpx;
                    text-align: center;
                    line-height: 1.4;
                }
            }
        }
    }

    .datetime-picker {
        display: flex;
        align-items: center;
        justify-content: start;
        width: 50%;
        margin-top: 32rpx;
    }
</style>